﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace ReportingManagement
{
    public partial class GetValue : Form
    {
        private DbType m_DataType = DbType.Object;
        private object value = null;

        public GetValue(DbType dataType, object value)
        {
            InitializeComponent();

            m_DataType = dataType;
            this.value = value;
            EnableControl();
        }

        public GetValue(DbType dataType, object value, string subTitle)
        {
            InitializeComponent();

            m_DataType = dataType;
            this.value = value;
            lblSubtitle.Text = subTitle;
            EnableControl();
        }

        public string SubTitle
        {
            get
            {
                return lblSubtitle.Text;
            }
            set
            {
                lblSubtitle.Text = value;
            }
        }

        private void EnableControl()
        {
            switch (m_DataType)
            {
                case DbType.AnsiString:
                case DbType.AnsiStringFixedLength:
                case DbType.String:
                case DbType.StringFixedLength:
                    if (value != null) textBox.Text = value.ToString();
                    textBox.Visible = true;
                    break;                

                case DbType.Boolean:
                    if (value != null)
                    {
                        try
                        {
                            bool yes = Boolean.Parse(value.ToString());
                            rdoTrue.Checked = yes;
                        }
                        catch { }
                    }
                    groupBox1.Visible = true;

                    break;

                case DbType.Byte:
                    numericUpDown.Minimum = Byte.MinValue;
                    numericUpDown.Maximum = Byte.MaxValue;
                    numericUpDown.Increment = 1;
                    numericUpDown.DecimalPlaces = 0;
                    if (value != null)
                    {
                        try
                        {
                            numericUpDown.Value = Byte.Parse(value.ToString());
                        }
                        catch { }
                    }
                    numericUpDown.Visible = true;
                    break;

                case DbType.Date:
                    dateTimePicker.Format = DateTimePickerFormat.Short;
                    if (value != null)
                    {
                        try
                        {
                            dateTimePicker.Value = DateTime.Parse(value.ToString());
                        }
                        catch { }
                    }
                    dateTimePicker.Visible = true;
                    break;

                case DbType.DateTime:
                case DbType.DateTime2:
                case DbType.DateTimeOffset:
                    dateTimePicker.Format = DateTimePickerFormat.Long;
                    if (value != null)
                    {
                        try
                        {
                            dateTimePicker.Value = DateTime.Parse(value.ToString());
                        }
                        catch { }
                    }
                    dateTimePicker.Visible = true;
                    break;

                case DbType.Currency:
                case DbType.Decimal:
                case DbType.Double:
                case DbType.Single:
                    numericUpDown.Minimum = Decimal.MinValue;
                    numericUpDown.Maximum = Decimal.MaxValue;
                    numericUpDown.Increment = 1;
                    numericUpDown.DecimalPlaces = 4;
                    if (value != null)
                    {
                        try
                        {
                            numericUpDown.Value = Decimal.Parse(value.ToString());
                        }
                        catch { }
                    }
                    numericUpDown.Visible = true;
                    break;    

                case DbType.Int16:
                    numericUpDown.Minimum = Int16.MinValue;
                    numericUpDown.Maximum = Int16.MaxValue;
                    numericUpDown.Increment = 1;
                    numericUpDown.DecimalPlaces = 0;
                    if (value != null)
                    {
                        try
                        {
                            numericUpDown.Value = Int16.Parse(value.ToString());
                        }
                        catch { }
                    }
                    numericUpDown.Visible = true;
                    break;

                case DbType.Int32:
                    numericUpDown.Minimum = Int32.MinValue;
                    numericUpDown.Maximum = Int32.MaxValue;
                    numericUpDown.Increment = 1;
                    numericUpDown.DecimalPlaces = 0;
                    if (value != null)
                    {
                        try
                        {
                            numericUpDown.Value = Int32.Parse(value.ToString());
                        }
                        catch { }
                    }
                    numericUpDown.Visible = true;
                    break;

                case DbType.Int64:
                    numericUpDown.Minimum = Int64.MinValue;
                    numericUpDown.Maximum = Int64.MaxValue;
                    numericUpDown.Increment = 1;
                    numericUpDown.DecimalPlaces = 0;
                    if (value != null)
                    {
                        try
                        {
                            numericUpDown.Value = Int64.Parse(value.ToString());
                        }
                        catch { }
                    }
                    numericUpDown.Visible = true;
                    break;

                case DbType.UInt16:
                    numericUpDown.Minimum = UInt16.MinValue;
                    numericUpDown.Maximum = UInt16.MaxValue;
                    numericUpDown.Increment = 1;
                    numericUpDown.DecimalPlaces = 0;
                    if (value != null)
                    {
                        try
                        {
                            numericUpDown.Value = UInt16.Parse(value.ToString());
                        }
                        catch { }
                    }
                    numericUpDown.Visible = true;
                    break;

                case DbType.UInt32:
                    numericUpDown.Minimum = UInt32.MinValue;
                    numericUpDown.Maximum = UInt32.MaxValue;
                    numericUpDown.Increment = 1;
                    numericUpDown.DecimalPlaces = 0;
                    if (value != null)
                    {
                        try
                        {
                            numericUpDown.Value = UInt32.Parse(value.ToString());
                        }
                        catch { }
                    }
                    numericUpDown.Visible = true;
                    break;

                case DbType.UInt64:
                    numericUpDown.Minimum = UInt64.MinValue;
                    numericUpDown.Maximum = UInt64.MaxValue;
                    numericUpDown.Increment = 1;
                    numericUpDown.DecimalPlaces = 0;
                    if (value != null)
                    {
                        try
                        {
                            numericUpDown.Value = UInt64.Parse(value.ToString());
                        }
                        catch { }
                    }

                    numericUpDown.Visible = true;
                    break;                

                case DbType.SByte:
                    numericUpDown.Minimum = SByte.MinValue;
                    numericUpDown.Maximum = SByte.MaxValue;
                    numericUpDown.Increment = 1;
                    numericUpDown.DecimalPlaces = 0;
                    if (value != null)
                    {
                        try
                        {
                            numericUpDown.Value = SByte.Parse(value.ToString());
                        }
                        catch { }
                    }
                    numericUpDown.Visible = true;
                    break;

                case DbType.Time:
                    dateTimePicker.Format = DateTimePickerFormat.Time;
                    if (value != null)
                    {
                        try
                        {
                            dateTimePicker.Value = DateTime.Parse(value.ToString());
                        }
                        catch { }
                    }
                    dateTimePicker.Visible = true;
                    break;

                case DbType.Guid:
                case DbType.Object:
                case DbType.VarNumeric:
                case DbType.Xml:
                case DbType.Binary:
                default:  
                    textBox.Multiline = true;
                    textBox.Height = 50;
                    textBox.Width = 200;
                    if (value != null) textBox.Text = value.ToString();
                    textBox.Visible = true;
                    break;
            }
        }

        public object Value
        {
            get
            {
                return value;
            }
        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            if (textBox.Visible)
            {
                if (String.IsNullOrEmpty(textBox.Text))
                {
                    MessageBox.Show(this, "Please enter some text");
                    return;
                }
                else value = textBox.Text;
            }
            if (numericUpDown.Visible) value = numericUpDown.Value;
            if (dateTimePicker.Visible) value = dateTimePicker.Value;
            if (groupBox1.Visible)
            {
                if (rdoFalse.Checked || rdoTrue.Checked) value = rdoTrue.Checked;
                else
                {
                    MessageBox.Show(this, "Please select a value");
                    return;
                }
            }
            this.DialogResult = System.Windows.Forms.DialogResult.OK;
            this.Close();
        }
    }
}
